home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / C / LIB / OSLIB / EXAMPLES / h / x < prev   
Text File  |  1995-09-20  |  4KB  |  146 lines

  1. #ifndef x_H
  2. #define x_H
  3.  
  4. /*x.h - exception handling header*/
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <setjmp.h>
  9. #include <signal.h>
  10.  
  11. #ifndef os_H
  12.    #include "os.h"
  13. #endif
  14.  
  15. #ifndef trace_H
  16.    #include "trace.h"
  17. #endif
  18.  
  19. typedef
  20.    struct x_exception
  21.       {  int trampoline [3];
  22.          void (*previous [3]) (int);
  23.          os_error *error;
  24.          jmp_buf buf;
  25.       }
  26.       x_exception;
  27.  
  28. /* (Pseudo-)function to be called before a group of statements which
  29.  * could throw an exception.
  30.  */
  31.  
  32. #define x_TRY(x) \
  33.       if (x__try (x), setjmp ((x)->buf) == NULL)
  34. extern void x__try (x_exception *);
  35.  
  36. /* Function to allow the exception type to be examined. MUST be
  37.  * called to balance a call to x_TRY.
  38.  */
  39.  
  40. #define x_CATCH(x) \
  41.       if (x__catch (x), (x)->error != NULL)
  42. extern void x__catch (x_exception *);
  43.  
  44. /* Function to re-throw exceptions. Used in exception handlers or in tidy-
  45.  * up code to throw the error to the next-higher level that needs it. If
  46.  * there was no exception, this does nothing.
  47.  */
  48.  
  49. #define x_RETHROW(x) \
  50.       ((x)->error != NULL? os_generate_error ((x)->error): (void) SKIP)
  51.  
  52. /* Strange pseudo-declaration thing to make an |os_error *| from an error
  53.  * number and a message. Declares |id| to be an |os_error *| pointing at the
  54.  * block {num, mess}, without wasting 252 bytes of empty error block. Can
  55.  * also be used if you want an international message that will be passed to
  56.  * messagetrans_error_lookup.
  57.  */
  58.  
  59. #define x_LOCAL_ERROR(id, num, mess) \
  60.    static struct {int errnum; char errmess [sizeof mess + 1];} id ## _ = \
  61.          {num, mess}; \
  62.    static os_error *id = (os_error *) &id ## _;
  63.  
  64. /* Another declaration, to make an |os_error *| from an error
  65.  * number and a message. Declares |id| to be an |os_error *| pointing at the
  66.  * block {num, mess}, without wasting 252 bytes of empty error block. Can
  67.  * also be used if you want an international message that will be passed to
  68.  * messagetrans_error_lookup.
  69.  */
  70.  
  71. #define x_GLOBAL_ERROR(id, num, mess) \
  72.    static struct {int errnum; char errmess [sizeof mess + 1];} id ## _ = \
  73.          {num, mess}; \
  74.    os_error *id = (os_error *) &id ## _;
  75.  
  76. /* Function to throw exceptions. Used anywhere that obtains an
  77.  * |os_error *| (usually from x_LOCAL_ERROR), and wants to turn it into an
  78.  * exception. Undefined if its argument is NULL.
  79.  */
  80.  
  81. #define x_THROW(error) \
  82.    os_generate_error (error)
  83.  
  84. /* Function to throw an error after one has been detected by the run-time
  85.  * system. Use after (e g) fopen, printf fail.
  86.  */
  87.  
  88. #define x_THROW_LAST_ERROR() \
  89.    os_generate_error (x__last_error ())
  90. extern os_error *x__last_error (void);
  91.  
  92. /* Function to give the error responsible. Useful only at the top, where the
  93.  * error is be reported.
  94.  */
  95.  
  96. #define x_ERROR(x) ((x)->error)
  97.  
  98. /* Function to exit from the programme, with an appropriate message and
  99.  * return value (not useful for desktop programmes).
  100.  */
  101.  
  102. #define x_EXIT(x) \
  103.    (  (x)->error != NULL? \
  104.       (  fprintf (stderr, "%s\n", (x)->error->errmess), \
  105.          exit (EXIT_FAILURE) \
  106.       ): \
  107.       exit (0) \
  108.    )
  109.  
  110. /* Function to exit from a desktop programme with an appropriate message and
  111.  * return value.
  112.  */
  113.  
  114. #define x_REPORT_EXIT(x, programme) \
  115.    (  (x)->error != NULL? \
  116.       (  wimp_report_error ((x)->error, NONE, programme), \
  117.          exit (EXIT_FAILURE) \
  118.       ): \
  119.       exit (0) \
  120.    )
  121.  
  122. /* A set of functions to do exception-throwing memory allocation. If they
  123.  * return, they have succeeded, so they can be used in expressions, etc.
  124.  */
  125.  
  126. extern os_error *x_no_memory;
  127.  
  128. #if TRACE
  129.    #define x_ALLOC(size)                  x__alloc (size)
  130.    #define x_CALLOC(count, size)          x__calloc (count, size)
  131.    #define x_FREE(ptr, size)              x__free (ptr, size)
  132.    #define x_REALLOC(ptr, old_size, size) x__realloc (ptr, old_size, size)
  133. #else
  134.    #define x_ALLOC(size)                  x__alloc (size)
  135.    #define x_CALLOC(count, size)          x__calloc (count, size)
  136.    #define x_FREE(ptr, size)              x__free (ptr, 0)
  137.    #define x_REALLOC(ptr, old_size, size) x__realloc (ptr, 0, size)
  138. #endif
  139.  
  140. extern void *x__alloc (int size);
  141. extern void *x__calloc (int count, int size);
  142. extern void x__free (void *ptr, int size);
  143. extern void *x__realloc (void *ptr, int old_size, int size);
  144.  
  145. #endif
  146.